import time
from time import sleep


class Awaitable(object):
    def __init__(self, obj):
        self.value = obj

    def __await__(self):
        yield self


class Task(object):
    def __init__(self, coro):
        self.coro = coro
        self._done = False
        self._result = None

    def run(self):
        if not self._done:
            try:
                x = self.coro.send(None)
            except StopIteration as e:
                self._result = e.value
                self._done = True
            else:
                assert isinstance(x, Awaitable)
        else:
            print('task is done')


async def small_step():
    print('休息一下，马上回来')
    t1 = time.time()
    await Awaitable((sleep, 2))

    assert time.time() - t1 > 2, "睡眠时间不足"
    print('努力工作中')
    return 123


async def big_step():
    sr = await small_step()


if __name__ == '__main__':
    t = Task(big_step())
    t.run()
